#include "paramssetapi.h"

ParamsSetApi::ParamsSetApi(QObject *parent)
    : QObject{parent}
{
    baseUrl = Config::baseUrl;

    manager = new QNetworkAccessManager(0);
    request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/json"));//post请求设置为json，才能发送josn格式文件
    request.setRawHeader("Authorization",  qApp->property("token").value<QString>().toLocal8Bit());

}

QNetworkReply *ParamsSetApi::list(int pageNum, int pageSize, QString configName,
                                  QString configKey, QString configType,
                                  QString beginTime, QString endTime){
    QString params = "system/config/list?pageNum="+ QString::number(pageNum) +
                     "&pageSize=" + QString::number(pageSize);
    if(!configName.isEmpty()){
        params += "&configName=" + configName;
    }
    if(!configKey.isEmpty()){
        params += "&configKey=" + configKey;
    }
    if(!configType.isEmpty()){
        params += "&configType=" + configType;
    }
    if(beginTime != "2000/1/1" && endTime != "2000/1/1"){
        params += "&params%5BbeginTime%5D=" + beginTime + "&params%5BendTime%5D=" + endTime;
    }
    request.setUrl(QUrl(baseUrl + params));
    QNetworkReply* reply = manager->get(request);

    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();

    return reply;
}

QNetworkReply *ParamsSetApi::sys_yes_no()
{
    request.setUrl(baseUrl + "system/dict/data/type/sys_yes_no");
    QNetworkReply * r = manager->get(request);
    //同步请求
    QEventLoop eventLoop;
    connect(r, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    return r;
}

QNetworkReply *ParamsSetApi::deleteItems(QString ids)
{

    request.setUrl(baseUrl + "system/config/" + ids);
    QNetworkReply * r = manager->deleteResource(request);
    //同步请求
    QEventLoop eventLoop;
    connect(r, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    return r;
}

QNetworkReply *ParamsSetApi::getById(QString id)
{
    request.setUrl(baseUrl + "system/config/" + id);
    QNetworkReply * r = manager->get(request);
    //同步请求
    QEventLoop eventLoop;
    connect(r, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    return r;
}

QNetworkReply *ParamsSetApi::update(SysConfig *s)
{
    QJsonObject obj = s->toJsonObject();
    QJsonDocument doc(obj);
    QByteArray data = doc.toJson();
    request.setUrl(baseUrl + "system/config/");
    QNetworkReply * r = manager->put(request,data);
    //同步请求
    QEventLoop eventLoop;
    connect(r, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    return r;
}

QNetworkReply *ParamsSetApi::add(SysConfig *s)
{
    QJsonObject obj = s->toJsonObject();
    QJsonDocument doc(obj);
    QByteArray data = doc.toJson();
    request.setUrl(baseUrl + "system/config/");
    QNetworkReply * r = manager->post(request,data);
    //同步请求
    QEventLoop eventLoop;
    connect(r, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    return r;
}

QNetworkReply *ParamsSetApi::refreshCache()
{
    request.setUrl(baseUrl + "system/dict/type/refreshCache");
    QNetworkReply * r = manager->deleteResource(request);
    //同步请求
    QEventLoop eventLoop;
    connect(r, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    return r;
}


